/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2005, 2008. All Rights Reserved. 
 * 
 * Note to U.S. Government Users Restricted Rights:  Use, 
 * duplication or disclosure restricted by GSA ADP Schedule 
 * Contract with IBM Corp.
 *******************************************************************************/

package org.eclipsecon.dui.interpreter.internal;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class ModelEditor extends EditorPart {
	private IEditorInput fInput;
	private ModelContainer fContainer;
	private boolean fIsDirty = false;

	private ModelContext context = new ModelContext();

	public ModelEditor() {
	}

	/*
	 * @seeorg.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
	 * IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
	}

	/*
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	@Override
	public void doSaveAs() {
	}

	/*
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
	 * org.eclipse.ui.IEditorInput)
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		fInput = input;
		setSite(site);
		setInput(input);
	}

	/*
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return fIsDirty;
	}

	/*
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	@Override
	public boolean isSaveAsAllowed() {
		return true;
	}

	/*
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		throw new UnsupportedOperationException("Implement");
		// fContainer = new ModelContainer(context);
		// fContainer.create(parent);
		//
		// if (fInput instanceof IPathEditorInput) {
		// IPath path = ((IPathEditorInput) fInput).getPath();
		// fContainer.load(URI.createPlatformResourceURI(path.toString(),
		// true));
		// listenToModify(parent);
		// }
		//
		// IEditorSite editorSite = (IEditorSite) getSite();
		// editorSite.getActionBars().getToolBarManager().add(
		// new ReloadAction(fContainer));
		// editorSite.getActionBars().getToolBarManager().add(new Separator());
		// editorSite.getActionBars().getToolBarManager().add(
		// new BindAction(fContainer, context, getSite().getShell()));
	}

	private void listenToModify(Control control) {
		if (control instanceof Text) {
			((Text) control).addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					fIsDirty = true;
					firePropertyChange(PROP_DIRTY);
				}
			});
		}

		else if (control instanceof Combo) {
			((Combo) control).addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					fIsDirty = true;
					firePropertyChange(PROP_DIRTY);
				}
			});
		}

		else if (control instanceof Composite) {
			Control[] children = ((Composite) control).getChildren();
			for (Control child : children) {
				listenToModify(child);
			}
		}
	}

	/*
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
	}
}